/**
 * @license http://www.gnu.org/licenses/agpl-3.0.html The GNU Affero General Public License V3.0
 * From https://github.com/revoltchat/frontend/blob/30abe9801eaac3268a8fe0df04b4987acd7d734d/lifecycle/generateEmojiMapping.mjs
 * and code modified for our needs.
 */

/**
 * This script transforms emoji metadata (from the emoji-metadata project)
 * into a simplified JavaScript mapping used for emoji search/match.
 *
 * Download the emoji JSON file from:
 * https://github.com/googlefonts/emoji-metadata
 *
 * Then change the import file below (optional) and run this script with:
 *   node generateEmojiMapping.js
 */

import { writeFile } from 'node:fs/promises';
import emojis from './emojis.json' with { type: 'json' };

const mapping = [];

for (const group of Object.keys(emojis)) {
  for (const emote of emojis[group].emoji) {
    const emoji = String.fromCodePoint(...emote.base);
    const emoticons = emote.emoticons?.length ? ` ${emote.emoticons.join(' ')}` : '';

    for (const shortcode of emote.shortcodes) {
      const match = shortcode
        .slice(1, shortcode.length -1)
        .replace(/ /g, '-')
        .toLowerCase();

      mapping.push({
        value: emoji,
        match: match + emoticons
      });
    }
  }
}

const content = `// Generated by generateEmojiMapping.js — do not edit manually.\n` +
                `export default ${JSON.stringify(mapping)};\n`;

await writeFile('emoji.js', content, 'utf8');
